<template>
  <div class="container">
    <div class="content">
      <div class="left">
        <img src="../assets/img/loginleft.png" alt="">
      </div>
      <div class="right">
        <div class="languageContainer">
          <div class="language">
            <div class="pic">
              <img :src="$i18n.locale==='ch'?zhongguo:yingguo" alt="">
            </div>
            <div class="text">{{$i18n.locale==='ch'?"中文":"ENGLISH"}}</div>
            <div class="icon" @click="langFlag=!langFlag">
              <img src="../assets/img/back@2x.png" alt="">
            </div>
          </div>
          <div class="languageSelect" v-show="langFlag" @click="languagechange">
            <div class="pic">
              <img :src="$i18n.locale==='ch'?yingguo:zhongguo" alt="">
            </div>
            <div class="desc">{{$i18n.locale==='ch'?"ENGLISH":"中文"}}</div>
          </div>
        </div>
        <div class="logo"><img src="../assets/img/logo.png" alt=""></div>
        <div class="title">VTRS{{$t('home.title')}}2.0</div>
        <div class="formContainer">
          <div class="item">
            <i class="icon iconPhone"></i>
            <label for="">{{$t('home.account')}}：</label>
            <input type="text" :placeholder="$t('home.inputPhone')"  v-model="account">
          </div>
          <div class="item">
            <i class="icon iconPassword"></i>
            <label for="">{{$t('home.password')}}：</label>
            <input type="password" :placeholder="$t('home.inputPassword')" v-model="password">
          </div>
          <div class="itemBtn">
            <button type="button" class="login" @click="login">{{$t('home.login')}}</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {get} from "../assets/js/http";

export default {
  name: "login",
  data() {
    return {
      account: "",
      password: "",
      zhongguo:require('../assets/img/zhongguo-2@2x.png'),
      yingguo:require('../assets/img/yingguo-2@2x.png'),
      langFlag:false,
    }
  },
  mounted() {
    // 绑定enter事件
    this.enterKeyup();
    this.account = this.$cookies.get("account") ? this.$cookies.get("account") : "";
    this.password = this.$cookies.get("pass") ? this.$cookies.get("pass") : "";
  },
  destroyed() {
    // 销毁enter事件
    this.enterKeyupDestroyed();
  },
  methods: {
    languagechange(){
      if (this.$i18n.locale==="ch"){
        this.$i18n.locale = 'zh';
      }else{
        this.$i18n.locale = 'ch';
      }
    },
    enterKey(event) {
      const code = event.keyCode
          ? event.keyCode
          : event.which
              ? event.which
              : event.charCode;
      if (code == 13) {
        this.login();
      }
    },
    enterKeyupDestroyed() {
      document.removeEventListener("keyup", this.enterKey);
    },
    enterKeyup() {
      document.addEventListener("keyup", this.enterKey);
    },
    login() {
      if (this.account === "") {
        this.$message({
          message: "账号不能为空！",
          type: 'warning'
        });
      } else if (this.password === "") {
        this.$message({
          message: "密码不能为空",
          type: 'warning'
        });
      }
      get("https://course.acktechnologies.com.cn/api/user/login", {
        account: this.account,
        password: this.password
      })
          .then(res => {
            console.log(res)
            this.show = false;
            if (res.code === 1) {
              this.$cookies.set("vtrstoken", res.data.userinfo.token);
              this.$cookies.set("account", this.account);
              this.$cookies.set("pass", this.password);
              this.$cookies.set("avatar", res.data.userinfo.avatar);
              this.$cookies.set("nickname", res.data.userinfo.nickname);
              this.$router.push("./");
            } else {
              this.$message({
                message: res.msg,
                type: 'warning'
              });
            }
          })
          .catch(error => {
            // error
            this.$message({
              message: error.msg,
              type: 'warning'
            });
            console.log(error);
          });
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 100%;
  height: 100%;
  background: url("../assets/img/loginbg.png") no-repeat;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;

  .content {
    width: 860px;
    height: 524px;
    display: flex;
    flex-direction: row;

    .left {
      width: 466px;
      height: 100%;

      img {
        display: block;
        width: 100%;
        height: 100%;
      }
    }

    .right {
      width: 400px;
      height: 516px;
      background-color: #FFFFFF;
      box-shadow: 0px 5px 9px 0px rgba(6, 82, 251, 0.18);

      .logo {
        width: 55px;
        height: 44px;
        margin: 72px auto 9px auto;

        img {
          display: block;
          width: 100%;
          height: 100%;
        }
      }

      .title {
        height: 18px;
        font-size: 18px;
        font-family: SourceHanSansCN-Medium, SourceHanSansCN;
        font-weight: 500;
        color: #333333;
        line-height: 18px;
        letter-spacing: 1px;
      }

      .formContainer {
        margin-top: 72px;
        overflow: hidden;
        .item {
          width: 300px;
          margin: 20px auto 0 auto;
          display: flex;
          flex-direction: row;
          align-items: center;
          justify-content: left;
          border: 1px solid #D2D2D2;
          color: #D0D0D0;
          padding-left: 15px;
          box-sizing: border-box;

          label {
            display: block;
            text-align: left;
            padding-left: 6px;
            box-sizing: border-box;
          }

          .icon {
            display: block;
            width: 12px;
            height: 12px;
          }

          .iconPhone {
            background: url("../assets/img/zhanghu.png") no-repeat center center/100%;
          }

          .iconPassword {
            background: url("../assets/img/mima.png") no-repeat center center/100%;
          }

          input {
            width: 215px;
            height: 40px;
            background: #FFFFFF;
            border-radius: 2px;
            border: none;
            font-size: 14px;
            font-family: SourceHanSansCN-Normal, SourceHanSansCN;
            font-weight: 400;
            color: #333333;
            line-height: 40px;
            letter-spacing: 1px;
            box-sizing: border-box;
          }

          input::-webkit-input-placeholder {
            color: #D0D0D0;
          }

          input::-moz-placeholder {
            color: #D0D0D0;
          }

          input:-ms-input-placeholder {
            color: #D0D0D0;
          }

        }

        .itemBtn {
          margin-top: 70px;
        }

        .login {
          width: 300px;
          height: 40px;
          background: linear-gradient(270deg, #2379FF 0%, #67A2FF 100%);
          border-radius: 2px;
          font-size: 18px;
          font-family: SourceHanSansCN-Regular, SourceHanSansCN;
          font-weight: 400;
          color: #FFFFFF;
          line-height: 40px;
          letter-spacing: 3px;
          border: none;
          cursor: pointer;
        }
      }
    }
  }
}
.languageContainer{
  margin: 20px;
  float: right;
  position: relative;
}
.language{
  display: flex;
  flex-direction: row;
  align-items: center;
  cursor: pointer;
  .pic{
    width: 30px;
    img{
      display: block;
      width: 100%;
    }
  }
  .text{
    font-size: 16px;
    font-family: SourceHanSansCN-Normal, SourceHanSansCN;
    color: #666666;
    letter-spacing: 1px;
    margin: 0 10px;
  }
  .icon{
    width: 12px;
    img{
      display: block;
      width: 100%;
    }
  }
}
.languageSelect{
  position: absolute;
  right: -55px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  width: 120px;
  height: 55px;
  padding-top: 10px;
  background: url("../assets/img/juxing.png")no-repeat center center/100%;
  cursor: pointer;
  .pic{
    width: 27px;
    margin-right: 10px;
    img{
      display: block;
      width: 100%;
    }
  }
  .desc{
    font-size: 14px;
    color: #999999;
    letter-spacing: 1px;
  }
}
</style>